from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QWidget
from ui_Widget import Ui_Widget

class QmyWidget(QWidget):
    def __init__(self, parent = None):
        super().__init__(parent)
        self.ui = Ui_Widget()
        self.ui.setupUi(self)

    #   类变量
    __ADict = {
        "GCU" : ["A", "Ala", "丙氨酸"],
        "GCC" : ["A", "Ala", "丙氨酸"],
        "GCA" : ["A", "Ala", "丙氨酸"],

        "CGU" : ["R", "Arg", "精氨酸"],
        "CGA" : ["R", "Arg", "精氨酸"],
        "CGC" : ["R", "Arg", "精氨酸"],
        "CGG" : ["R", "Arg", "精氨酸"],
        "AGA" : ["R", "Arg", "精氨酸"],
        "AGG" : ["R", "Arg", "精氨酸"],

        "AAU" : ["N", "Asn", "天冬酰胺"],
        "AAC" : ["N", "Asn", "天冬酰胺"],

        "GAU" : ["D", "Asp", "天冬氨酸"],
        "GAC" : ["D", "Asp", "天冬氨酸"],

        "UGU" : ["C", "Cys", "半胱氨酸"],
        "UGC" : ["C", "Cys", "半胱氨酸"],

        "CAA" : ["Q", "Gln", "谷氨酰胺"],
        "CAG" : ["Q", "Gln", "谷氨酰胺"],

        "GAA" : ["E", "Glu", "谷氨酸"],
        "GAG" : ["E", "Glu", "谷氨酸"],

        "GGU" : ["G", "Gly", "甘氨酸"],
        "GGC" : ["G", "Gly", "甘氨酸"],
        "GGA" : ["G", "Gly", "甘氨酸"],
        "GGG" : ["G", "Gly", "甘氨酸"],

        "CAU" : ["H", "His", "组氨酸"],
        "CAC" : ["H", "His", "组氨酸"],

        "AUU" : ["I", "Ile", "异亮氨酸"],
        "AUC" : ["I", "Ile", "异亮氨酸"],
        "AUA" : ["I", "Ile", "异亮氨酸"],

        "CUU" : ["L", "Leu", "亮氨酸"],
        "CUC" : ["L", "Leu", "亮氨酸"],
        "CUA" : ["L", "Leu", "亮氨酸"],
        "CUG" : ["L", "Leu", "亮氨酸"],

        "AAA" : ["K", "Lys", "赖氨酸"],
        "AAG" : ["K", "Lys", "赖氨酸"],

        "AUG" : ["M", "Met", "甲硫氨酸（蛋氨酸）"],

        "UUU" : ["F", "Phe", "苯丙氨酸"],
        "UUC" : ["F", "Phe", "苯丙氨酸"],

        "CCU" : ["P", "Pro", "脯氨酸"],
        "CCC" : ["P", "Pro", "脯氨酸"],
        "CCA" : ["P", "Pro", "脯氨酸"],
        "CCG" : ["P", "Pro", "脯氨酸"],

        "UCA" : ["S", "Ser", "丝氨酸"],
        "UCC" : ["S", "Ser", "丝氨酸"],
        "UCA" : ["S", "Ser", "丝氨酸"],
        "UCG" : ["S", "Ser", "丝氨酸"],
        "AGU" : ["S", "Ser", "丝氨酸"],
        "AGC" : ["S", "Ser", "丝氨酸"],

        "ACU" : ["T", "Thr", "苏氨酸"],
        "ACC" : ["T", "Thr", "苏氨酸"],
        "ACA" : ["T", "Thr", "苏氨酸"],
        "ACG" : ["T", "Thr", "苏氨酸"],

        "UGG" : ["W", "Trp", "色氨酸"],

        "UAU" : ["Y", "Tyr", "酪氨酸"],
        "UAC" : ["Y", "Tyr", "酪氨酸"],

        "GUU" : ["V", "Val", "缬氨酸"],
        "GUC" : ["V", "Val", "缬氨酸"],
        "GUA" : ["V", "Val", "缬氨酸"],
        "GUA" : ["V", "Val", "缬氨酸"],

        "UGA" : ["U", "Sec", "硒代半胱氨酸"],

        "UAG" : ["O", "Pyl", "吡咯赖氨酸"]
    }

    #   DNA->RNA
    @pyqtSlot()
    def on_Btn1_clicked(self):
        text_old = self.ui.TxtBox.toPlainText()
        text_old = text_old.upper()
        text_new = str()
        for i in text_old:
            if i not in ["A", "T", "G", "C"]:
                text_new += "?"
            elif i == "T":
                text_new += "U"
            else:
                text_new += i
        self.ui.TxtBox.setText(text_new)

    #   RNA->蛋白质
    @pyqtSlot()
    def on_Btn2_clicked(self):
        text_old = self.ui.TxtBox.toPlainText()
        text_old = text_old.upper()
        text_new = str()
        for i in range(len(text_old)):
            if (i+1)%3 != 0:
                continue
            else:
                if text_old[i-2: i+1] in self.__ADict:
                    text_new += self.__ADict[text_old[i-2: i+1]][1]
                else:
                    text_new += "?__"
        self.ui.TxtBox.setText(text_new)

    #   DNA->蛋白质
    @pyqtSlot()
    def on_Btn3_clicked(self):
        self.on_Btn1_clicked()
        self.on_Btn2_clicked()
        
    #   清除
    @pyqtSlot()
    def on_Btnclear_clicked(self):
        self.ui.TxtBox.clear()